package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationService relationService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void updateCascadeById(BrandEntity brand) {
        // 更新品牌数据
        this.updateById(brand);

        // 更新品牌分类关联表中的品牌名称
        if (StringUtils.isNotBlank(brand.getName())) {
            CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
            relationEntity.setBrandId(brand.getBrandId());
            relationEntity.setBrandName(brand.getName());

            relationService.update(
                    relationEntity,
                    new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", relationEntity.getBrandId()));
        }
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity relationEntity) {
        Long brandId = relationEntity.getBrandId();
        Long catelogId = relationEntity.getCatelogId();

        // 获取品牌的名称
        BrandEntity brand = this.getById(brandId);
        String brandName = brand.getName();
        // 获取分类名称
        CategoryEntity category = categoryService.getById(catelogId);
        String categoryName = category.getName();

        relationEntity.setBrandName(brandName);
        relationEntity.setCatelogName(categoryName);

        relationService.save(relationEntity);
    }

    @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
        List<CategoryBrandRelationEntity> relationEntities = relationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        return relationEntities.stream().map(entity -> {
            // 通过分类品牌关联关系，获取品牌的详细信息
            return this.getById(entity.getBrandId());
        }).collect(Collectors.toList());
    }
}